#######################################
# Path to the SDL1.3 directory
SDLPATH=../../../../SDL-1.3.0-6248
#######################################


#######################################
# Project config:
BIN:=Fridge
SRCDIR=../../Source
BINDIR=../../linux
SRCEXT=cpp
PLATFORM=Linux
#######################################


#######################################
# Make:
CFLAGS=-g -fpermissive -Wno-write-strings \
		-I$(SRCDIR)/Independent \
		-I$(SRCDIR)/$(PLATFORM) \
		-I$(SDLPATH)/include/ \
		`pkg-config gtk+-2.0 --cflags`

LDFLAGS=-L$(SDLPATH)/build/.libs \
		-lSDL \
		-lSDL_ttf \
		-lpng \
		-lGL \
		-lasound \
		-larchive \
		`pkg-config lua --libs 2> /dev/null || pkg-config lua5.1 --libs` \
		`pkg-config gtk+-2.0 --libs`

SOURCES := $(shell cd $(SRCDIR); find Independent -name '*.$(SRCEXT)') \
			$(shell cd $(SRCDIR); find $(PLATFORM) -name '*.$(SRCEXT)')
SRCDIRS := $(shell find . -name '*.$(SRCEXT)' -exec dirname {} \; | uniq)

DEBUG_BINDIR = $(BINDIR)/debug
DEBUG_OBJDIR = $(DEBUG_BINDIR)/obj
DEBUG_OBJS = $(patsubst %.$(SRCEXT),$(DEBUG_OBJDIR)/%.o,$(SOURCES))

RELEASE_BINDIR = $(BINDIR)/release
RELEASE_OBJDIR = $(RELEASE_BINDIR)/obj
RELEASE_OBJS = $(patsubst %.$(SRCEXT),$(RELEASE_OBJDIR)/%.o,$(SOURCES))


# debug is default
all : debug

### DEBUG ###
debug : BUILDTYPE=debug
debug : $(DEBUG_BINDIR)/$(BIN) copydata
$(DEBUG_BINDIR)/$(BIN) : buildrepo $(DEBUG_OBJS)
	@mkdir -p `dirname $@`
	@$(CXX) $(DEBUG_OBJS) $(LDFLAGS) -o $@

$(DEBUG_OBJDIR)/%.o : $(SRCDIR)/%.$(SRCEXT)
	@echo Building $<
	@mkdir -p `dirname $@`
	@$(CXX) -c $(CFLAGS) $< -o $@

### RELEASE ###
release : BUILDTYPE=release
release : CFLAGS+=-DFRIDGE_RELEASE
release : $(RELEASE_BINDIR)/$(BIN) copydata

$(RELEASE_BINDIR)/$(BIN) : buildrepo $(RELEASE_OBJS)
	@mkdir -p `dirname $@`
	@$(CXX) $(RELEASE_OBJS) $(LDFLAGS) -o $@

$(RELEASE_OBJDIR)/%.o : $(SRCDIR)/%.$(SRCEXT)
	@echo Building $<
	@mkdir -p `dirname $@`
	@$(CXX) -c $(CFLAGS) $< -o $@


### CLEAN ###
clean : objclean dataclean
	@echo Removing binaries
	@$(RM) -f $(BINDIR)/debug/$(BIN)
	@$(RM) -f $(BINDIR)/release/$(BIN)

objclean : 
	@echo Removing object files
	@$(RM) -rf $(BINDIR)/debug/obj
	@$(RM) -rf $(BINDIR)/release/obj

dataclean : 
	@echo Removing resources
	@$(RM) -rf $(BINDIR)/debug/Fonts $(BINDIR)/debug/Graphics $(BINDIR)/debug/Templates
	@$(RM) -rf $(BINDIR)/release/Fonts $(BINDIR)/release/Graphics $(BINDIR)/release/Templates


### UTILS ###
buildrepo :
	@$(call make-repo)

copydata : $(BINDIR)
	cp -r "$(BINDIR)/../Win32 Release/Fonts" "$(BINDIR)/$(BUILDTYPE)/Fonts"
	cp -r "$(BINDIR)/../Win32 Release/Graphics" "$(BINDIR)/$(BUILDTYPE)/Graphics"
	cp -r "$(BINDIR)/../Win32 Release/Templates" "$(BINDIR)/$(BUILDTYPE)/Templates"

.phony : clean objclean

define make-repo
   for dir in $(SRCDIRS); \
   do \
	mkdir -p $(OBJDIR)/$$dir; \
   done
endef

	# @$(call make-depend,$<,$@,$(subst .o,.d,$@))
# usage: $(call make-depend,source-file,object-file,depend-file)
define make-depend
  $(CXX) -MM        \
         -MF $3     \
         -MP        \
         -MT $2     \
         $(CFLAGS) \
         $1
endef
